﻿package top.yangyitao.util;

import top.yangyitao.model.Point;

public class MonteCarlo {
	public static Point getBestPoint(int[][] chessBoard,int AIFlag,int userFlag) {
		Point p = new Point();
		Point test = new Point();
		int highScore = 0;
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++) {
				if(chessBoard[i][j]!=0)
					continue;
				test.setY(i);
				test.setX(j);
				int testScore = getPointScore(chessBoard,test,AIFlag,userFlag);
				if(testScore>=highScore) {
					highScore = testScore;
					p.setX(test.getX());
					p.setY(test.getY());
					p.setPointScore(testScore);
				}
			}
		return p;
	}
	public static int getStateScore(int chessLinkLength,int space1,int space2,boolean isAI) {
		int stateScore = 0;
		if (space1 + space2 + chessLinkLength < 5)
	        return 0;
	    switch (chessLinkLength) {
	        case 1:
	        	return 0;
	        case 2:
	                stateScore += 50;
	                break;
	        case 3:
	        		if ((space1 != 0) && (space2 != 0))
	        			stateScore += 650;
	        		else
	        			stateScore += 60;
	        		break;
	        case 4:
	            if ((space2 == 0) || (space1 == 0))
					stateScore += 1250;
	            else if (isAI)
					stateScore += 3750;
	            else
					stateScore += 2500;
	            break;
	        default:
	            if (isAI) 
	                stateScore += 300000;
	            stateScore += 300000;
	    }
	    return stateScore;
	}
	public static int getPointScore(int[][] chessBoard,Point p,int AIFlag,int userFlag) {
		int pointScore = 0;
		int[][] shift = {{1,0},{1,1},{0,1},{1,-1}};
		int times = 1;
		chessBoard[p.getY()][p.getX()] = AIFlag;
		boolean isAI = true;
		Point p0 = new Point();
		Point p1 = new Point();
		do {
			for(int i=0;i<4;i++) {
				int chessLinkLength1 = getPointLinkLength(chessBoard,p,shift[i][0],shift[i][1],1,false);
				int chessLinkLength2 = getPointLinkLength(chessBoard,p,shift[i][0],shift[i][1],-1,false);				
				p0.setY(p.getY()+(chessLinkLength1+1)*shift[i][0]);
				p0.setX(p.getX()+(chessLinkLength1+1)*shift[i][1]);
				int space1 = IsIn(p0)?getPointLinkLength(chessBoard,p0,shift[i][0],shift[i][1],1,true)+1:0;				
				p1.setY(p.getY()-(chessLinkLength1+1)*shift[i][0]);
				p1.setX(p.getX()-(chessLinkLength1+1)*shift[i][1]);
				int space2 = IsIn(p1)?getPointLinkLength(chessBoard,p1,shift[i][0],shift[i][1],-1,true)+1:0;
				pointScore+=getStateScore(chessLinkLength1+chessLinkLength2+1,space1,space2,isAI);
			}
			times++;
			chessBoard[p.getY()][p.getX()] = userFlag;
			isAI = false;
		}while(times<=2);
		chessBoard[p.getY()][p.getX()] = 0;
		return pointScore;
	}
	public static int getPointLinkLength(int[][] chessBoard,Point p,int shift0,int shift1,int sign,boolean isSpace) {
		p = new Point(p.getX(),p.getY());
		int link = 0;
		int linkChess = chessBoard[p.getY()][p.getX()];
		int spaceFlag=0;
		if(isSpace)
			spaceFlag = chessBoard[p.getY()-sign*shift0][p.getX()-shift1*sign];
		p.setY(p.getY()+shift0*sign);
		p.setX(p.getX()+shift1*sign);
		while(IsIn(p)){
			if(chessBoard[p.getY()][p.getX()]!=linkChess) {
				if(!((isSpace)&&(chessBoard[p.getY()][p.getX()]==spaceFlag)))
					break;
			}
			link++;
			p.setY(p.getY()+shift0*sign);
			p.setX(p.getX()+shift1*sign);
		}
		return link;
	}
	public static boolean IsIn(Point p) {
		if(p.getX()<0||p.getX()>=15)
			return false;
		if(p.getY()<0||p.getY()>=15)
			return false;
		return true;
	}
}
